local formas = {}
formas["morelanterns_candil_techo"] =  {
	{-0.0625, -0.4375, 0.0625, 0.0625, -0.1875, 0.125}, -- axis
	{-0.0625, -0.5, -0.0625, 0.0625, -0.4375, 0.125}, -- pie
	{-0.0625, -0.1875, 0.0625, 0.0625, -0.125, 0.1875}, -- handle
	{0.0625, -0.4375, 0, -0.0625, -0.375, -0.0625}, -- knot
};
formas["morelanterns_candil_pared"] = {
	{-0.0625, -0.4375, 0.0625, 0.0625, -0.375, 0.3125}, -- axis
	{-0.0625, -0.4375, 0, 0.0625, -0.25, 0.0625}, -- pie
	{-0.0625, -0.5, 0.3125, 0.0625, -0.375, 0.375}, -- handle
	{-0.0625, -0.3125, 0.0625, 0.0625, -0.25, 0.125}, -- knot
};
formas["morelanterns_candil_suelo"] = {
	{-0.0625, -0.4375, 0.0625, 0.0625, -0.1875, 0.125}, -- axis
	{-0.0625, -0.5, -0.0625, 0.0625, -0.4375, 0.125}, -- pie
	{-0.0625, -0.1875, 0.0625, 0.0625, -0.125, 0.1875}, -- handle
	{-0.0625, -0.4375, -0.0625, 0.0625, -0.375, 0}, -- knot
};

-- ------------------------------------------------

local function on_flood(pos, oldnode, newnode)
	minetest.add_item(pos, ItemStack("morelanterns:candil 1"))
	-- Play flame-extinguish sound if liquid is not an 'igniter'
	local nodedef = minetest.registered_items[newnode.name]
	if not (nodedef and nodedef.groups and
			nodedef.groups.igniter and nodedef.groups.igniter > 0) then
		minetest.sound_play(
			"default_cool_lava",
			{pos = pos, max_hear_distance = 16, gain = 0.07},
			true
		)
	end
	-- Remove the torch node
	return false
end

minetest.register_node("morelanterns:candil", {
	description = "Candil",
	drawtype = "mesh",
	mesh = "candil.obj",
	--inventory_image = "default_torch_on_floor.png",
	-- wield_image = "default_torch_on_floor.png",
	tiles = { "candil_mat.png"
	--	    name = "default_torch_on_floor_animated.png",
	--	    animation = {type = "vertical_frames", aspect_w = 16, aspect_h = 16, length = 3.3}
	},
	
	-- drawtype = "nodebox",
	-- node_box = {
	-- 	type = "fixed",
	-- 	fixed = formas["morelanterns_candil_suelo"]
	-- },

	--node_box = {
	--	type = "fixed",
	--	wall_top = formas["morelanterns_candil_techo"],
	--	wall_bottom = formas["morelanterns_candil_suelo"],
	--		wall_side = formas["morelanterns_candil"]
	--},

	inventory_image = "morelanterns_candil_inv.png",
	wield_image = "morelanterns_candil_inv.png",
	-- tiles = { "morelanterns_candil.png" },
	use_texture_alpha = "clip",
	paramtype = "light",
	paramtype2 = "wallmounted",
	sunlight_propagates = true,
	walkable = false,
	liquids_pointable = false,
	light_source = LIGHT_LEVELS.min,
	groups = {choppy=2, dig_immediate=3, flammable=1, attached_node=1, torch=1, lantern=1},
	drop = "morelanterns:candil",
	--selection_box = {
	--	type = "wallmounted",
--		wall_bottom = formas["morelanterns_candil_suelo"]
--	},
	sounds = default.node_sound_wood_defaults(), 
	
	on_place = function(itemstack, placer, pointed_thing)
		local under = pointed_thing.under
		local node = minetest.get_node(under)
		local def = minetest.registered_nodes[node.name]
		if def and def.on_rightclick and
			not (placer and placer:is_player() and
			placer:get_player_control().sneak) then
			-- minetest.chat_send_all("DENTRO IF")
			return def.on_rightclick(under, node, placer, itemstack,
				pointed_thing) or itemstack
		end
		-- minetest.chat_send_all("FUERA IF")
		local above = pointed_thing.above
		local wdir = minetest.dir_to_wallmounted(vector.subtract(under, above))
		local fakestack = itemstack
		if wdir == 0 then
			fakestack:set_name("morelanterns:candil_ceiling")
		elseif wdir == 1 then
			fakestack:set_name("morelanterns:candil")
		else
			fakestack:set_name("morelanterns:candil_wall")
		end

		itemstack = minetest.item_place(fakestack, placer, pointed_thing, wdir)
		itemstack:set_name("morelanterns:candil")

		return itemstack
	end,
	
	floodable = true,
	on_flood = on_flood,
	on_rotate = false
})

minetest.register_node("morelanterns:candil_wall", {
	drawtype = "mesh",
	mesh = "candil.obj",
	tiles = { "candil_mat.png"
	--	    name = "default_torch_on_floor_animated.png",
	--	    animation = {type = "vertical_frames", aspect_w = 16, aspect_h = 16, length = 3.3}
	},
	-- drawtype = "nodebox",
	-- node_box = {
	--	type = "fixed",
	--	fixed = formas["morelanterns_candil_pared"]
	--},
	--tiles = { "morelanterns_candil.png" },

	use_texture_alpha = "clip",
	paramtype = "light",
	paramtype2 = "wallmounted",
	sunlight_propagates = true,
	walkable = false,
	light_source = LIGHT_LEVELS.low,
	groups = {choppy=2, dig_immediate=3, flammable=1, not_in_creative_inventory=1, attached_node=1, torch=1, lantern=1},
	drop = "morelanterns:candil",
	--selection_box = {
	--	type = "wallmounted",
--		wall_side = formas["morelanterns_candil_pared"]
--	},
	sounds = default.node_sound_wood_defaults(),
	floodable = true,
	on_flood = on_flood,
	on_rotate = false
})

minetest.register_node("morelanterns:candil_ceiling", {
	 drawtype = "mesh",
	mesh = "candil.obj",
	tiles = { "candil_mat.png"
	--	    name = "default_torch_on_floor_animated.png",
	--	    animation = {type = "vertical_frames", aspect_w = 16, aspect_h = 16, length = 3.3}
	},
	--drawtype = "nodebox",
	--node_box = {
	--	type = "fixed",
	--	fixed = formas["morelanterns_candil_techo"]
	--},
	--tiles = { "morelanterns_candil.png" },

	use_texture_alpha = "clip",
	paramtype = "light",
	paramtype2 = "wallmounted",
	sunlight_propagates = true,
	walkable = false,
	light_source = LIGHT_LEVELS.med,
	groups = {choppy=2, dig_immediate=3, flammable=1, not_in_creative_inventory=1, attached_node=1, torch=1, lantern=1},
	drop = "morelanterns:candil",
	--selection_box = {
	--	type = "wallmounted",
	--	wall_top = formas["morelanterns_candil_techo"],
	--},
	sounds = default.node_sound_wood_defaults(),
	floodable = true,
	on_flood = on_flood,
	on_rotate = false
})

minetest.register_lbm({
	name = "morelanterns:3dcandil",
	nodenames = {"morelanterns:candil","morelanterns:candil_ceiling","morelanterns:candil_wall"},
	action = function(pos, node)
		if node.param2 == 0 then
			minetest.set_node(pos, {name = "morelanterns:candil_ceiling",
				param2 = node.param2})
		elseif node.param2 == 1 then
			minetest.set_node(pos, {name = "morelanterns:candil",
				param2 = node.param2})
		else
			minetest.set_node(pos, {name = "morelanterns:candil_wall",
				param2 = node.param2})
		end
	end
})


-- registrar crafteos

local register_linterna_craft = function(name, material)
    minetest.register_craft({
        output = name,
        recipe = {{material},
                  {"torch"},
                  {"default:glass"}
                }
    })
end

register_linterna_craft("morelanterns:candil","default:tin_ingot");

-- CRAFT CON ACEITE
local ethereal = minetest.get_modpath("ethereal");

if (ethereal) then

	minetest.register_craft({
		output="morelanterns:candil",
		recipe= {
			{"default:tin_ingot"},
			{"ethereal:olive_oil"},
			{"default:glass"}
		}
	})
end
